/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.google.common.base.Strings;
import com.openbrew.openbrewrf.Hops;
import com.openbrew.openbrewrf.Malt;
import com.openbrew.openbrewrf.POLines;
import com.openbrew.openbrewrf.PurchaseOrder;
import com.openbrew.openbrewrf.Supplies;
import com.openbrew.openbrewrf.Yeast;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import service.enumerations.WarehouseIngredient;

/**
 *
 * @author chapmand
 */
public class CreateSimplePurchaseOrderView {
    private List<Hops> hops = new ArrayList<Hops>();
    private List<Malt> malt = new ArrayList<Malt>();
    private List<Yeast> yeast = new ArrayList<Yeast>();
    private List<Supplies> supplies = new ArrayList<Supplies>();
    
    //Caching Functions
    
    private List<Hops> getHops(final EntityManager em){
        TypedQuery<Hops> query = em.createQuery(
                    "SELECT h FROM Hops h", Hops.class);
        return query.getResultList();
    }
    
    private List<Malt> getMalts(final EntityManager em){
        TypedQuery<Malt> query = em.createQuery(
                    "SELECT m FROM Malt m", Malt.class);
        return query.getResultList();
    }
    
    private List<Yeast> getYeasts(final EntityManager em){
        TypedQuery<Yeast> query = em.createQuery(
                    "SELECT y FROM Yeast y", Yeast.class);
        return query.getResultList();
    }
    
    private List<Supplies> getSupplies(final EntityManager em){
        TypedQuery<Supplies> query = em.createQuery(
                    "SELECT s FROM Supplies s", Supplies.class);
        return query.getResultList();
    }
        
    private Supplies getSupplyFromCache(final String supplyID){
        if (Strings.isNullOrEmpty(supplyID)) {
                throw new IllegalArgumentException("Empty Supply ID");
        }
        for (final Supplies sup : supplies) {
                if (supplyID.equals(Integer.toString(sup.getSupID()))) {
                        return sup;
                }
        }
        throw new IllegalArgumentException("Unknown ID: " + supplyID);
    }

    private Hops getHopFromCache(final String hopID) {
        if (Strings.isNullOrEmpty(hopID)) {
                throw new IllegalArgumentException("Empty Hop ID");
        }
        for (final Hops hopItem : hops) {
                if (hopID.equals(Integer.toString(hopItem.getHid()))) {
                        return hopItem;
                }
        }
        throw new IllegalArgumentException("Unknown ID: " + hopID);
    }
        
    private Malt getMaltFromCache(final String maltID) {
        if (Strings.isNullOrEmpty(maltID)) {
                throw new IllegalArgumentException("Empty Malt ID");
        }
        for (final Malt maltItem : malt) {
                if (maltID.equals(Integer.toString(maltItem.getMaltID()))) {
                        return maltItem;
                }
        }
        throw new IllegalArgumentException("Unknown ID: " + maltID);
    }

    private Yeast getYeastFromCache(final String yeastID) {
        if (Strings.isNullOrEmpty(yeastID)) {
                throw new IllegalArgumentException("Empty Yeast ID");
        }
        for (final Yeast yeastItem : yeast) {
                if (yeastID.equals(Integer.toString(yeastItem.getYid()))) {
                        return yeastItem;
                }
        }
        throw new IllegalArgumentException("Unknown ID: " + yeastID);
    }
    
    private void loadCache(final EntityManager em){
        hops = getHops(em);
        malt = getMalts(em);
        yeast = getYeasts(em);
        supplies = getSupplies(em);
    }
    
    public SimplePurchaseOrderView GetSalesOrderString(final PurchaseOrder purchaseOrder, final EntityManager em) {
        String overViewString = "";
        loadCache(em);
        float totalCost = 0;
        for (final POLines poLine : purchaseOrder.getPOLinesCollection()) {
            float elementTotalCost = 0;
            final NumberFormat formatter = new DecimalFormat("$###.##");
            
            // variables for use.
            final int tid = poLine.getPolpn().getTableID();
            final int qty = poLine.getPOLQty();
            final WarehouseIngredient warehouseId = WarehouseIngredient.create(poLine.getPolpn().getWhid().getWearhouseID());

            if(warehouseId != null){
		switch (warehouseId) {
                    case HOPS:
                        final Hops hop = getHopFromCache(Integer.toString(tid));
                        elementTotalCost = hop.getHopCost()*hop.getHopBagLbs()*(float)qty;
                        overViewString += hop.getHopName() + " (" + Float.toString(hop.getHopBagLbs())
                                + " Lbs. Bag)," + Integer.toString(qty) + ","
                                + formatter.format(hop.getHopCost()*hop.getHopBagLbs()) + ","
                                + formatter.format(elementTotalCost) + "\n";
                        totalCost += elementTotalCost;
                        break;
                    case MALT_AND_ADJUNCTS:
                        final Malt malt = getMaltFromCache(Integer.toString(tid));
                        elementTotalCost = malt.getMaltCost()*malt.getMaltBagLbs()*(float)qty;
                        overViewString += malt.getMaltName() + "," 
                                + Integer.toString(qty) + ","
                                + formatter.format(malt.getMaltCost()*malt.getMaltBagLbs()) + ","
                                + formatter.format(elementTotalCost) + "\n";
                        totalCost += elementTotalCost;
                        break;
                    case SPICES_AND_FLAVORINGS:
                        final Supplies saf = getSupplyFromCache(Integer.toString(tid));
                        elementTotalCost = saf.getSupPrice()*(float)qty;
                        String itemNum = "";
                        if(!saf.getSupNumber().equals("")){
                            itemNum = "(Item #: " + saf.getSupNumber() + ")";
                        }
                        overViewString += saf.getSupName() + itemNum + ","
                                + Integer.toString(qty) + ","
                                + formatter.format(saf.getSupPrice()) + ","
                                + formatter.format(elementTotalCost) + "\n";
                        totalCost += elementTotalCost;
                        break;
                    case YEAST:
                        final Yeast yeast = getYeastFromCache(Integer.toString(tid));
                        elementTotalCost = yeast.getYeastCost()*(float)qty;
                        overViewString += yeast.getYeastDesc() + "," 
                                + Integer.toString(qty) + ","
                                + formatter.format(yeast.getYeastCost()) + ","
                                + formatter.format(elementTotalCost) + "\n";
                        totalCost += elementTotalCost;
                        break;
                    default:
                        final Supplies supply = getSupplyFromCache(Integer.toString(tid));
                        elementTotalCost = supply.getSupPrice()*(float)qty;
                        String itemNumS = "";
                        if(!supply.getSupNumber().equals("")){
                            itemNumS = "(Item #: " + supply.getSupNumber() + ")";
                        }
                        overViewString += supply.getSupName() + itemNumS + ","
                                + Integer.toString(qty) + ","
                                + formatter.format(supply.getSupPrice()) + ","
                                + formatter.format(elementTotalCost) + "\n";
                        totalCost += elementTotalCost;
                        break;     
		}
            } else {
                final Supplies supply = getSupplyFromCache(Integer.toString(tid));
                elementTotalCost = supply.getSupPrice()*(float)qty;
                String itemNumS = "";
                if(!supply.getSupNumber().equals("")){
                    itemNumS = "(Item #: " + supply.getSupNumber() + ")";
                }
                overViewString += supply.getSupName() + itemNumS + ","
                        + Integer.toString(qty) + ","
                        + formatter.format(supply.getSupPrice()) + ","
                        + formatter.format(elementTotalCost) + "\n";
                totalCost += elementTotalCost;
            }
        }
        return new SimplePurchaseOrderView(totalCost, overViewString, 0);
    }
}
